package com.sgb.merchant.view.fragment;

import android.graphics.Color;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.fondesa.recyclerviewdivider.DividerDecoration;
import com.sgb.link.entity.merchant.IndustryDetailEntity;
import com.sgb.merchant.R;
import com.sgb.merchant.databinding.MerchantIndustryInfoBinding;
import com.sgb.merchant.viewmodel.IndustryViewModel;
import com.swgk.core.common.base.BaseFragment;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.ImagesAdapter;
import com.swgk.core.dialog.ImagesPreviewDialog;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 王彬臣
 * @date: 2021/10/13
 * @Description: 产业园区基本信息
 */
public class IndustryInfoFragment extends BaseFragment<IndustryViewModel, MerchantIndustryInfoBinding> {

    IndustryDetailEntity mEntity;
    ImagesAdapter picAdapter;
    List<String> mUrls = new ArrayList<>();
    ArrayList<String> url;
    List<IndustryDetailEntity.ParkLabelsBean> mTagList = new ArrayList<>();
    private TagAdapter<IndustryDetailEntity.ParkLabelsBean> tagAdapter;

    public void setEntity(IndustryDetailEntity mEntity) {
        this.mEntity = mEntity;

    }

    @Override
    protected int layoutId() {
        return R.layout.merchant_industry_info;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(IndustryViewModel.class);

    }

    @Override
    protected void init() {
        mBinding.rvImg.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        picAdapter = new ImagesAdapter(mUrls);
        if (mBinding.rvImg.getItemDecorationCount() == 0) {
            DividerDecoration.builder(getActivity())
                    .color(Color.TRANSPARENT)
                    .size(10, TypedValue.COMPLEX_UNIT_DIP)
                    .build()
                    .addTo(mBinding.rvImg);
        }

        picAdapter.setOnItemClickListener((adapter, view, position) -> {
            ImagesPreviewDialog.newInstance(url, position).show(getActivity());

        });
        picAdapter.bindToRecyclerView(mBinding.rvImg);
        tagAdapter = new TagAdapter<IndustryDetailEntity.ParkLabelsBean>(mTagList) {
            @Override
            public View getView(FlowLayout parent, int position, IndustryDetailEntity.ParkLabelsBean parkLabelsBean) {
                final LayoutInflater mInflater = LayoutInflater.from(getActivity());
                LinearLayout layout = (LinearLayout) mInflater.inflate(R.layout.merchant_item_facility, mBinding.tagList, false);
                TextView tvTag = layout.findViewById(R.id.tv_info);
                tvTag.setText(parkLabelsBean.getLabelName());
                return layout;
            }
        };
        mBinding.tagList.setAdapter(tagAdapter);
        if(mEntity!=null){
            mBinding.setViewModel(mEntity);
            mUrls.clear();
            if (mEntity.getParkImages() != null) {
                mUrls.addAll(mEntity.getParkImages());
                picAdapter.notifyDataSetChanged();
                url = (ArrayList<String>) mUrls;
                if (mEntity.getParkImages().size() == 0) {
                    mBinding.llPic.setVisibility(View.GONE);
                }else{
                    mBinding.llPic.setVisibility(View.VISIBLE);
                }
            }
            mTagList.clear();
            if (mEntity.getParkLabels() != null) {
                mTagList.addAll(mEntity.getParkLabels());
            }
            tagAdapter.notifyDataChanged();
        }
    }

    public void refresh(IndustryDetailEntity mEntity){
        this.mEntity = mEntity;
        if(mEntity!=null){
            mBinding.setViewModel(mEntity);
            mUrls.clear();
            if (mEntity.getParkImages() != null) {
                mUrls.addAll(mEntity.getParkImages());
                picAdapter.notifyDataSetChanged();
                url = (ArrayList<String>) mUrls;
                if (mEntity.getParkImages().size() == 0) {
                    mBinding.llPic.setVisibility(View.GONE);
                }
            }
            mTagList.clear();
            if (mEntity.getParkLabels() != null) {
                mTagList.addAll(mEntity.getParkLabels());
            }
            tagAdapter.notifyDataChanged();
        }
    }

    @Override
    public void onClick(View v) {

    }
}
